import React from 'react';
import PropTypes from 'prop-types';
import {Button, Form, Row, Col} from 'antd';
import {FORM_OPTIONS} from '../constants';
import {PERMISSION} from './constants';
import TextInput from 'Shared/components/TextInput';
import DealerSelectPanel from '../common/DealerSelect/SelectEdit';
import DateRangePicker from 'Shared/components/DateRangePicker';
import {hasPermissions} from './utils';
import {injectIntl} from './intl';
import ImportPanel from './ImportPanel';
import routes from './routes';
import {dealerType} from '../Enum';
import Export from '../common/Export';
const FormItem = Form.Item;
const dealerConditions = {
    type: dealerType.总代
};
const subDealerConditions = {
    type: dealerType.经销商
};
class QueryPanel extends React.PureComponent {
    state = {
        showImportPanel: false
    }
    handleFilterChange = (value, name) => {
        this.props.onConditionsChange(name, value);
    }
    handleFilterPressEnter = (value, name) => {
        this.props.onConditionsChange(name, value);
        this.props.querySearch();
    }
    onSelectDealer = data => {
        this.props.onConditionsChange('dealerId', data.id);
        this.props.onQueryDealerChange({
            id: data.id,
            code: data.code,
            name: data.name,
        });
    }
    onSelectSubDealer = data => {
        this.props.onConditionsChange('subDealerId', data.id);
        this.props.onQuerySubDealerChange({
            id: data.id,
            code: data.code,
            name: data.name,
        });
    }
    onChangeImport = () => {
        this.setState({
            showImportPanel: true
        });
    }
    onImportChange = data => {
        if(data)
            this.setState({
                showImportPanel: false
            });
    }
    onCloseImportPanel = () => {
        this.setState({
            showImportPanel: false
        });
    }
    render() {
        const operations = [];
        const dealerInfo = this.props.conditions.dealerId ? {
            id: this.props.queryDealer.id,
            code: this.props.queryDealer.code,
            name: this.props.queryDealer.name
        } : null;
        const subDealerInfo = this.props.conditions.subDealerId ? {
            id: this.props.querySubDealer.id,
            code: this.props.querySubDealer.code,
            name: this.props.querySubDealer.name
        } : null;
        if(hasPermissions(this.props.permissions, PERMISSION.import) && !this.props.isCompanyUser)
            operations.push(
                <Button key={PERMISSION.import} onClick={this.onChangeImport}>
                    {this.props.intl.formatMessage({
                        id: 'queryPanel.btn.import',
                        defaultMessage: '导入'
                    })}
                </Button>
            );
        if(hasPermissions(this.props.permissions, PERMISSION.add))
            operations.push(<Button key="add" type="primary" href={`#${routes.add.url()}`}>{this.props.intl.formatMessage({
                id: 'queryPanel.operation.add',
                defaultMessage: '新增'
            })}</Button>);
        return (
            <div>
                <Form className="form-standard">
                    <Row>
                        <Col {...FORM_OPTIONS.col}>
                            <FormItem
                                label={this.props.intl.formatMessage({
                                    id: 'queryPanel.form.vin',
                                    defaultMessage: 'VIN'
                                })}
                                {...FORM_OPTIONS.item}>
                                <TextInput
                                    name="vin"
                                    value={this.props.conditions.vin}
                                    onPressEnter={this.handleFilterPressEnter}
                                    onBlur={this.handleFilterChange} />
                            </FormItem>
                        </Col>
                        {this.props.isCompanyUser && !this.props.enterprise && <Col {...FORM_OPTIONS.col}>
                            <FormItem
                                label={this.props.intl.formatMessage({
                                    id: 'queryPanel.form.dealerId',
                                    defaultMessage: '总代'
                                })}
                                {...FORM_OPTIONS.item}>
                                <DealerSelectPanel
                                    conditions={dealerConditions}
                                    value={dealerInfo}
                                    onSelect={this.onSelectDealer} />
                            </FormItem>
                        </Col>}
                        {!this.props.enterprise && <Col {...FORM_OPTIONS.col}>
                            <FormItem
                                label={this.props.intl.formatMessage({
                                    id: 'queryPanel.form.subDealerId',
                                    defaultMessage: '经销商'
                                })}
                                {...FORM_OPTIONS.item}>
                                <DealerSelectPanel
                                    conditions={subDealerConditions}
                                    value={subDealerInfo}
                                    onSelect={this.onSelectSubDealer} />
                            </FormItem>
                        </Col>}
                        <Col {...FORM_OPTIONS.col}>
                            <FormItem
                                label={this.props.intl.formatMessage({
                                    id: 'queryPanel.form.maintainDate',
                                    defaultMessage: '保养日期'
                                })}
                                {...FORM_OPTIONS.item}>
                                <DateRangePicker name="maintainDate" value={this.props.conditions.maintainDate}
                                    onChange={this.handleFilterChange} />
                            </FormItem>
                        </Col>
                    </Row>
                    <Row>
                        <Col span={6}>
                            <Button key="query" type="primary" onClick={this.props.querySearch} loading={this.props.loading}>
                                {this.props.intl.formatMessage({
                                    id: 'queryPanel.btn.query',
                                    defaultMessage: '查询'
                                })}
                            </Button>
                            <Button key="clear" onClick={this.props.onResetClick}>
                                {this.props.intl.formatMessage({
                                    id: 'queryPanel.btn.reset',
                                    defaultMessage: '重置'
                                })}
                            </Button>
                        </Col>
                        <Col span={18} className="col-align-right">
                            {hasPermissions(this.props.permissions, PERMISSION.export) &&
                            <Export key={PERMISSION.export} exportButton={this.props.intl.formatMessage({
                                id: 'queryPanel.btn.export',
                                defaultMessage: '导出'
                            })} exportRequest={this.props.onClickExport} />}
                            {hasPermissions(this.props.permissions, PERMISSION.settlementExport) &&
                            <Export key={PERMISSION.settlementExport} exportButton={this.props.intl.formatMessage({
                                id: 'queryPanel.btn.settlementExport',
                                defaultMessage: '结算导出'
                            })} exportRequest={this.props.onClickSettlementExport} />}
                            {operations}
                        </Col>
                    </Row>
                </Form>
                {
                    this.state.showImportPanel &&
                    <ImportPanel
                        selectKey={'1'}
                        onCancel={this.onCloseImportPanel}
                        onImportChange={this.onImportChange} />
                }
            </div>
        );
    }
}

QueryPanel.propTypes = {
    conditions: PropTypes.object.isRequired,
    loading: PropTypes.bool.isRequired,
    permissions: PropTypes.array.isRequired,
    querySearch: PropTypes.func.isRequired,
    onClickExport: PropTypes.func.isRequired,
    onClickSettlementExport: PropTypes.func.isRequired,
    onConditionsChange: PropTypes.func.isRequired,
    onQueryDealerChange: PropTypes.func.isRequired,
    onResetClick: PropTypes.func.isRequired,
    enterprise: PropTypes.object,
    intl: PropTypes.object,
    isCompanyUser: PropTypes.bool,
    queryDealer: PropTypes.object,
};

import {connect} from 'react-redux';
import {
    saveQueryCondition,
    resetQueryPanel,
    querySearch,
    saveQueryDealer,
    saveQuerySubDealer,
    exportData,
    settlementExportData
} from './actions';
import selectorFactory from 'Shared/utils/immutableToJsSelectorFactory';

const getConditions = selectorFactory(['page', 'appState', 'queryCondition']);
const getQueryDealer = selectorFactory(['page', 'appState', 'queryDealer']);
const getQuerySubDealer = selectorFactory(['page', 'appState', 'querySubDealer']);
const getPermissions = selectorFactory(['page', 'domainData', 'permission', 'data']);
const getEnterprise = selectorFactory(['page', 'domainData', 'initData', 'enterprise']);
const mapStateToProps = state => ({
    loading: state.getIn((['page', 'domainData', 'list', 'isFetching'])),
    isCompanyUser: state.getIn((['page', 'domainData', 'initData', 'isCompanyUser'])),
    conditions: getConditions(state),
    queryDealer: getQueryDealer(state),
    querySubDealer: getQuerySubDealer(state),
    enterprise: getEnterprise(state),
    permissions: getPermissions(state)
});

const mapDispatchToProps = dispatch => ({
    onConditionsChange: (name, value) => dispatch(saveQueryCondition(name, value)),
    onQueryDealerChange: data => dispatch(saveQueryDealer(data)),
    onQuerySubDealerChange: data => dispatch(saveQuerySubDealer(data)),
    onClickExport: () => dispatch(exportData()),
    querySearch: () => dispatch(querySearch()),
    onResetClick: () => dispatch(resetQueryPanel()),
    onClickSettlementExport: () => dispatch(settlementExportData()),
});
export default connect(mapStateToProps, mapDispatchToProps)(injectIntl(QueryPanel));
